package com.cms.common;


import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.cms.constant.CmsConstant;
import com.cms.model.TrAdminUser;

 

public class SiteUtil { 
 
	/***
	 * 获取ip地址信息
	 * @param request
	 * @return
	 */
	public static String getIpAddr(HttpServletRequest request) {  
	    String ip = request.getHeader("x-forwarded-for");  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getHeader("WL-Proxy-Client-IP");  
	    }  
	    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
	        ip = request.getRemoteAddr();  
	    }  
	    return ip;    
	}  
	
	
	/***
	 * 获取客户端操作系统  
	 * @param request
	 * @return
	 */
	public static String getOperationSystem(HttpServletRequest request) {  
		ClientInfo info =new ClientInfo(request);
		StringBuilder sb = new StringBuilder();
		sb.append(info.getOSName());
		sb.append(" ");
		sb.append(info.getOSVer());
		return sb.toString();
	}  
	
	
	/***
	 * 获取客户端操作系统  navigator
	 * @param request
	 * @return
	 */
	public static String getNavigator(HttpServletRequest request) { 		
		ClientInfo info =new ClientInfo(request);
		return info.getExplorerName();
	} 
	
	 /***
	  * 
	  * @return
	  */
	 public static HttpServletRequest getRequest()
	 {
		 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		 return request;
	 }
	 
	 
	 /***
	  * 
	  * @return
	  */
	 public static String getActionAddr()
	 {
		 HttpServletRequest request =getRequest();
		 return getIpAddr(request);
	 }
	 
	 
	 /***
	  * 
	  * @return
	  */
	 public static String getRealPath()
	 {
		 HttpServletRequest request = getRequest();
		 return request.getSession().getServletContext().getRealPath("/");
	 }
	 
	 
	 /***
	  * 
	  * @return
	  */
	 public static TrAdminUser getCurrentUser()
	 {
		 HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		 return (TrAdminUser)request.getSession().getAttribute(CmsConstant.CURRENT_USER);
	 }
}
